package com.anuo.app.modules.study.studynetwork;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

/**
 * 研究状态: 常用已完成
 * 参考:https://www.journaldev.com/7146/apache-httpclient-example-closeablehttpclient
 */
public class StudyApacheHttpClient {
    private static final String USER_AGENT = "Mozilla/5.0";

    private static final String GET_URL = "http://localhost:9090/SpringMVCExample";

    private static final String POST_URL = "http://localhost:9090/SpringMVCExample/home";

    public static void main(String[] args) throws IOException {
        //sendGET();
        System.out.println("GET DONE");
        sendPOST();
        System.out.println("POST DONE");
    }

    /**
     * 具体解释参考 下面的 sendPOST 方法
     * @throws IOException
     */
    private static void sendGET() throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(GET_URL);
        //加header
        httpGet.addHeader("User-Agent", USER_AGENT);
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

        //获取响应的状态码
        System.out.println("GET Response Status:: "
                + httpResponse.getStatusLine().getStatusCode());

        //获取响应的字符串
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent()));

        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        reader.close();
        //获取到响应字符串
        String responseStr=response.toString();
        // print result
        System.out.println(responseStr);
        httpClient.close();
    }

    /**
     * 发送 post 请求, 如有疑问参考下面的详细注释
     * @throws IOException
     */
    private static void sendPOST() throws IOException {

        //创建httppost对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(POST_URL);
        //添加header
        httpPost.addHeader("User-Agent", USER_AGENT);
        //添加 query string 比如: ?name=jk&age=27 或者参考: https://stackoverflow.com/questions/14043843/how-to-add-parameters-to-all-httpclient-request-methods
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("userName", "Pankaj Kumar"));

        HttpEntity postParams = new UrlEncodedFormEntity(urlParameters);
        httpPost.setEntity(postParams);

        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

        //获取响应的状态码
        System.out.println("POST Response Status:: "
                + httpResponse.getStatusLine().getStatusCode());

        //获取响应的字符串
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent()));

        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        reader.close();

        // print result
        System.out.println(response.toString());
        httpClient.close();

    }
}
